package com.cong.taoyao.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

@Controller
@RequestMapping
public class FilePreviewController {

    @GetMapping("/preview/{filename}")
    public String previewFile(@PathVariable String filename, Model model) {
        try {
            // 获取文件内容
            String content = Files.readString(Path.of("src/main/resources/templates/" + filename));

            // 将内容传递给前端
            model.addAttribute("content", content);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "preview";
    }
}
